﻿Imports System.Windows.Forms
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports Microsoft.Win32
Imports System.IO

Public Class Shipping_Record
    Public AttachFile As String 'The whole path
    Public Attach_FileName As String 'The name of the file and extension
    Public Attach_File_Ext As String 'Just the extension of the file
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click

        'Check to see if filename has been specified
        If TB_FilePath.Text <> "" Then
            'Assign Filename to Attach_Filename and to NewRecord form
            'Find name of file and the extension
            Dim AttachFile_Path As String() 'String() is a string array
            Dim FileName As String
            Dim ary_FileExtension As String()
            Dim Extension As String

            AttachFile_Path = AttachFile.Split(New Char() {"\"c}) 'New Char() is an array. Here we are saying the string array words, will hold each piece of the attach filepath string that will be split by the \ character

            For i = 0 To UBound(AttachFile_Path)

                If i = UBound(AttachFile_Path) Then

                    FileName = AttachFile_Path(i)
                    Attach_FileName = FileName
                    NewRecord_Form.TB_AttachFile.Text = FileName
                    ary_FileExtension = FileName.Split(New Char() {"."c})

                    For j = 0 To UBound(ary_FileExtension)

                        If j = UBound(ary_FileExtension) Then

                            'Assign Extension of file
                            Extension = ary_FileExtension(j)
                            Attach_File_Ext = Extension
                            Me.DialogResult = System.Windows.Forms.DialogResult.OK
                            Me.Close()
                        End If

                    Next

                End If

            Next

        Else
            MsgBox("Please enter a valid File Path and Name", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)
        End If
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Shipping_Record_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TB_FilePath.Enabled = False
    End Sub

    Private Sub BTN_BrowseFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_BrowseFile.Click
        'Display file open dialog to let user choose file
        OpenFD.FileName = ""
        OpenFD.Filter = "Supported File Types|*.txt; *.xls; *.xlsx; *.jpeg; *.gif; *.jpg; *.bmp; *.pdf; *.doc; *.docx; *.htm; *.html"
        Dim FD_Result As Integer = OpenFD.ShowDialog()

        If FD_Result = DialogResult.Cancel Then
            GoTo End_Sub
        End If

        'Assigns Full path and name of file to the variable AttachFile
        AttachFile = OpenFD.FileName
        TB_FilePath.Text = AttachFile
End_Sub:
    End Sub

    Private Sub BTN_Skip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Skip.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
    End Sub

    Private Sub BTN_Previous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Previous.Click
        ShippingRecord_Prev = "Y"
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
End Class
